package com.dingwei.superiordiary.adapter.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.text.method.LinkMovementMethod;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bobomee.android.mentions.edit.util.FormatRangeManager;
import com.bobomee.android.mentions.text.MentionTextView;
import com.bumptech.glide.Glide;
import com.dingwei.superiordiary.R;
import com.dingwei.superiordiary.app.MyApplication;
import com.dingwei.superiordiary.bean.MyhistoryPostsBean;
import com.dingwei.superiordiary.parser.Parser;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.lihao.baselibrary.base.BaseActivity;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by lihao on 2018/1/11.
 */

public class MyHistoryPostsAdapter extends RecyclerArrayAdapter<MyhistoryPostsBean.DataBean.ListBean> {


    private FormatRangeManager mRangeManager;
    private Parser mTagParser = new Parser();

    public MyHistoryPostsAdapter(Context context) {
        super(context);
        mRangeManager = new FormatRangeManager();
    }

    @Override
    public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyCollectHolder(parent);
    }


    class MyCollectHolder extends BaseViewHolder<MyhistoryPostsBean.DataBean.ListBean> {


        @Bind(R.id.img_head)
        ImageView imgHead;
        @Bind(R.id.tv_nick_name)
        TextView tvNickName;
        @Bind(R.id.tv_level)
        TextView tvLevel;
        @Bind(R.id.tv_add_time)
        TextView tvAddTime;
        @Bind(R.id.tv_content)
        MentionTextView tvContent;
        @Bind(R.id.tv_posts_type)
        TextView tvPostsType;
        @Bind(R.id.img_size)
        TextView imgSize;
        @Bind(R.id.img_layout)
        LinearLayout imgLayout;
        @Bind(R.id.img_video)
        ImageView imgVideo;
        @Bind(R.id.right)
        LinearLayout right;
        @Bind(R.id.tv_forward_name)
        TextView tvForwardName;
        @Bind(R.id.tv_forward_add_time)
        TextView tvForwardAddTime;
        @Bind(R.id.tv_forward_content)
        MentionTextView tvForwardContent;
        @Bind(R.id.img_forward_size)
        TextView imgForwardSize;
        @Bind(R.id.img_forward_layout)
        LinearLayout imgForwardLayout;
        @Bind(R.id.img_forward_video)
        ImageView imgForwardVideo;
        @Bind(R.id.forward_layout)
        LinearLayout forwardLayout;
        @Bind(R.id.tv_collect_time)
        TextView tvCollectTime;
        @Bind(R.id.rowFG)
        LinearLayout rowFG;
        @Bind(R.id.tv_attention)
        TextView tvAttention;

        public MyCollectHolder(ViewGroup parent) {
            super(parent, R.layout.item_my_history_posts);
            ButterKnife.bind(this, itemView);
        }

        @Override
        public void setData(MyhistoryPostsBean.DataBean.ListBean data) {
            super.setData(data);
            tvContent.setMovementMethod(new LinkMovementMethod());
            tvContent.setParserConverter(mTagParser);
            tvContent.setHighlightColor(BaseActivity.context.getResources().getColor(android.R.color.transparent));
            Glide.with(BaseActivity.context)
                    .load(data.portrait)
                    .apply(MyApplication.headOptions)
                    .into(imgHead);

            tvNickName.setText(data.nickname);
            tvLevel.setText("LV." + data.level);
            tvAddTime.setText(data.activity_addtime);
            if (data.attention == 0) {
                tvAttention.setText("+关注");
                tvAttention.setTextColor(ContextCompat.getColor(BaseActivity.context, R.color.theme_color));
                tvAttention.setBackgroundResource(R.drawable.theme_line_bg);
            } else {
                tvAttention.setText("已关注");
                tvAttention.setTextColor(ContextCompat.getColor(BaseActivity.context, R.color.white));
                tvAttention.setBackgroundResource(R.drawable.theme_bg);
            }
            String s = data.content.replaceAll("'>", "'>@");
            tvContent.setText(mRangeManager.getFormatCharSequence(s));

            tvPostsType.setVisibility(View.VISIBLE);
            if (data.type.equals("0")) {
                tvPostsType.setText(" 综合 ");
            } else if (data.type.equals("1")) {
                tvPostsType.setText("个人说");
            } else {
                tvPostsType.setVisibility(View.GONE);
            }
            if (data.data_type == 0) {
                imgVideo.setVisibility(View.GONE);
                imgLayout.setVisibility(View.GONE);
            } else if (data.data_type == 1) {
                imgVideo.setVisibility(View.GONE);
                imgLayout.setVisibility(View.VISIBLE);
                imgSize.setText("" + data.image_num);
            } else {
                imgVideo.setVisibility(View.VISIBLE);
                imgLayout.setVisibility(View.GONE);
            }

            if (data.transpond_type.equals("1")) {
                tvForwardName.setText(data.transpond_content.nickname);
                tvForwardContent.setText(data.transpond_content.content);
                tvForwardAddTime.setText(data.transpond_content.addtime);

                if (data.transpond_content.data_type == 0) {
                    imgForwardVideo.setVisibility(View.GONE);
                    imgForwardLayout.setVisibility(View.GONE);
                } else if (data.transpond_content.data_type == 1) {
                    imgForwardVideo.setVisibility(View.GONE);
                    imgForwardLayout.setVisibility(View.VISIBLE);
                    imgForwardSize.setText("" + data.image_num);
                } else {
                    imgForwardVideo.setVisibility(View.VISIBLE);
                    imgForwardLayout.setVisibility(View.GONE);
                }
            } else {
                forwardLayout.setVisibility(View.GONE);
            }
            tvCollectTime.setText(data.addtime);
        }
    }
}
